#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> nxt(n + 1, -1);
  for (int i = 1; i <= n; i ++) {
    int op, x, y;
    cin >> op;
    if (op == 1) {
      cin >> x;
      nxt[i] = nxt[x];
      nxt[x] = i;
    } else {
      cin >> x >> y;
      i64 ans1 = 0, ans2 = 0;
      int xx = nxt[x], yy = nxt[y];
      while (true) {
        if (xx != -1) {
          if (xx == y) {
            cout << ans1 << '\n';
            nxt[x] = y;
            break;
          } else {
            ans1 += xx;
            xx = nxt[xx];
          }
        }
        if (yy != -1) {
          if (yy == x) {
            cout << ans2 << '\n';
            nxt[y] = x;
            break;
          } else {
            ans2 += yy;
            yy = nxt[yy];
          }
        }
      }
    }
  }
}